package com.wisdytech.linkdcs.configuration.action;

import com.wisdytech.common.bean.ConfigurationData;
import com.wisdytech.common.bean.StandardConfigResult;
import com.wisdytech.common.enums.NgDataType;
import com.wisdytech.linkdcs.configuration.service.IConfigurationDesignService;
import com.wisdytech.linkdcs.configuration.service.ITtEqConfigurationDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 组态设计对外接口
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/equipment/configuration/design")
public class ConfigurationDesignAction {


    @Resource
    private IConfigurationDesignService configurationDesignService;

    @Resource
    private ITtEqConfigurationDataService ttEqConfigurationDataService;

    private Logger logger = LoggerFactory.getLogger(ConfigurationDesignAction.class);


    /**
     * 获取厂区下的所有产线和车间
     *
     * @param key 厂区id
     * @return StandardConfigResult
     */
    @RequestMapping(value = "/getShopAndLines/get.mvc")
    public StandardConfigResult getShopAndLines(String key) {
        logger.info("get lines and shops by factoryId[{}]", key);
        List<Map<String, Object>> result = configurationDesignService.getShopAndLines(key);
        return StandardConfigResult.getOkResult(result);
    }

    /**
     * 获取基础数据
     *
     * @param level   级别
     * @param factory 厂区
     * @return StandardConfigResult
     */
    @RequestMapping(value = "/getBasicData/get.mvc")
    public StandardConfigResult getBasicData(String level, String baseNo, String factory) {
        Map<String, Object> result = configurationDesignService.getBasicData(level, baseNo, factory);
        return StandardConfigResult.getOkResult(result);
    }

    /**
     * 获取基础数据
     *
     * @return StandardConfigResult
     */
    @RequestMapping(value = "/getHcAndBdEquipStatusInfo/get.mvc")
    public StandardConfigResult getHcAndBdEquipStatusInfo(String factoryId, String columnNo) {
        Map<String, Object> result = configurationDesignService.getHcAndBdEquipStatusInfo(factoryId, columnNo);
        return StandardConfigResult.getOkResult(result);
    }


    /**
     * @param fileKey 主键
     * @param name    文件名
     * @param type    类型
     * @return StandardConfigResult
     */
    @RequestMapping(value = "/getFileContent/get.mvc")
    public StandardConfigResult getFileContent(String fileKey, String name, String type) {
        logger.info(fileKey + " " + name + " " + type);
        ConfigurationData data = configurationDesignService.getFileContent(fileKey, name, type);
        return StandardConfigResult.getOkResult(data);
    }

    /**
     * 文件保存
     *
     * @param path    文件夹路径
     * @param fileKey 组态记录主键
     * @param name    文件名（包含文件扩展名）
     * @param content 文件内容
     * @param type    保存类型
     * @param equipNo 设备编码json
     * @return
     */
    @RequestMapping(value = "/saveFile/save.mvc")
    public StandardConfigResult saveFile(String path,
                                         String fileKey, String name, String content, String type, String equipNo, String center, String eye) {
        boolean result =
                configurationDesignService.saveFile(path, fileKey, name, content, type, equipNo, center, eye);

        return result ? StandardConfigResult.getOkResult("") :
                StandardConfigResult.getErrorResult(NgDataType.SCADA_ERROR.getCode(),
                        NgDataType.SCADA_ERROR.getName());

    }

    /**
     * 文件上传
     *
     * @param path
     * @return
     */
    @RequestMapping(value = "/uploadFile/save.mvc")
    public StandardConfigResult uploadFile(@RequestParam(value = "file", required = false) MultipartFile file,
                                           String path) {
        String fileName = file.getOriginalFilename();

        try {
            configurationDesignService.uploadFile(file.getBytes(), path, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return StandardConfigResult.getErrorResult(NgDataType.FILE_ERROR.getCode(),
                    NgDataType.FILE_ERROR.getName());
        }

        return StandardConfigResult.getOkResult("");
    }

    /**
     * 文件名检测
     *
     * @param path
     * @param name
     * @return
     */

    @RequestMapping(value = "/checkControlName/get.mvc")
    public StandardConfigResult checkControlName(String path, String name) {

        boolean result = configurationDesignService.checkControlName(path, name);
        return result ? StandardConfigResult.getErrorResult(NgDataType.SCADA_ERROR.getCode(),
                NgDataType.SCADA_ERROR.getName()) : StandardConfigResult.getOkResult("");
    }


    /**
     * 获取所有设备编号
     *
     * @return
     */
    @RequestMapping(value = "/getEquipCodes/get.mvc")
    public StandardConfigResult getEquipCodes() {

        List<Map<String, Object>> result =
                configurationDesignService.getEquipCodes();
        return StandardConfigResult.getOkResult(result);
    }

    /**
     * 获取设备所有参数
     *
     * @param equipNo
     * @return
     */
    @RequestMapping(value = "/getEquipParams/get.mvc")
    public StandardConfigResult getEquipParams(String equipNo) {
        List<Map<String, Object>> result =
                configurationDesignService.getEquipParams(equipNo);
        return StandardConfigResult.getOkResult(result);
    }

    /**
     * js文件获取
     *
     * @param path
     * @return
     */
    @RequestMapping(value = "/getFiles/get.mvc")
    public StandardConfigResult getFiles(String path, String type) {

        List<Map<String, Object>> result =
                configurationDesignService.getFiles(path, type);
        return StandardConfigResult.getOkResult(result);
    }

    @RequestMapping(value = "/generateItems/get.mvc")
    public StandardConfigResult generateItems(String path) {
        List<Map<String, Object>> result =
                configurationDesignService.getItemFils(path);
        return StandardConfigResult.getOkResult(result);
    }


    @RequestMapping(value = "/insertData/save.mvc")
    public StandardConfigResult insertData(String id) {
        ttEqConfigurationDataService.insertData(id);
        return StandardConfigResult.getOkResult(null);
    }


}
